<template>
  <form class="cmp cmp-sojson-decode row">
    <div class="col-5">
      <fieldset>
        <legend>输入</legend>
        <div class="fieldset-content">
          <textarea class="form-control" v-model.trim="inputContent" placeholder="请输入要处理的文本"></textarea>
        </div>
      </fieldset>
    </div>
    <div class="col-2">
      <div class="form-group-list">
        <div class="form-group">
          <div class="form-item-label">处理方式</div>
          <div class="form-item-input">
            <select class="form-control" v-model="decodeType">
              <option v-for="(v,i) in decodeTypeList" :key="i" :value="v.value">{{v.label}}</option>
            </select>
          </div>
        </div>
        <div class="form-group">
          <x-button type="primary" block @click="decrypt">解 密</x-button>
        </div>
      </div>
    </div>
    <div class="col-5">
      <fieldset>
        <legend>输出</legend>
        <div class="fieldset-content">
          <textarea class="form-control" v-model.trim="outputContent" placeholder="处理后的结果" readonly></textarea>
        </div>
      </fieldset>
    </div>
  </form>
</template>

<script>
import decodeFunction from './decode.js'
export default {
  name: 'sojson-decode',
  data () {
    return {
      inputContent: '',
      outputContent: '',
      decodeType: 'auto',
      decodeTypeList: [
        {value: 'auto', label: '自动识别'},
        {value: 'sojson4', label: 'sojson v4'},
        {value: 'sojson5', label: 'sojson v5(默认配置)'},
        {value: 'sojsonp', label: 'sojson高级版'},
        {value: 'jsjiami6', label: 'jsjiami v6(默认配置)'},
        {value: 'JJEncode', label: 'JJEncode'},
        {value: 'JSFuck', label: 'JSFuck'},
        {value: 'JSOB', label: 'JavaScript Obfuscator(普通版)'},
        {value: 'list', label: '列表混淆变量名'},
        {value: 'beautify', label: '美化'},
        {value: 'uglify', label: '压缩(UglifyJS)'},
        {value: 'eval', label: 'Eval(包含packer)'},
      ],
    }
  },
  methods: {
    // 表单校验
    checkForm () {
      let result = true
      if (!this.inputContent) {
        result = false
        this.$toast('请输入要处理的内容')
      }
      return result
    },
    // 解密
    decrypt () {
      if (!this.checkForm()) return
      this.outputContent = ''
      if (this.decodeType === 'auto') {
        this.outputContent = decodeFunction.autoscan(this.inputContent)
      } else if (this.decodeType === 'eval') {
        this.outputContent = decodeFunction.uneval(this.inputContent)
      } else if (this.decodeType === 'sojson4') {
        this.outputContent = decodeFunction.dec_sojson4(this.inputContent)
      } else if (this.decodeType === 'sojson5') {
        this.outputContent = decodeFunction.dec_sojsonv5_default(this.inputContent)
      } else if (this.decodeType === 'jsjiami6') {
        this.outputContent = decodeFunction.dec_jsjiamiv6_default(this.inputContent)
      } else if (this.decodeType === 'sojsonp') {
        this.outputContent = decodeFunction.decsojsonp(this.inputContent)
      } else if (this.decodeType === 'JJEncode') {
        this.outputContent = decodeFunction.jjdecode(this.inputContent)
      } else if (this.decodeType === 'JSFuck') {
        this.outputContent = decodeFunction.decjsf(this.inputContent)
      } else if (this.decodeType === 'JSOB') {
        this.outputContent = decodeFunction.obdec_default(this.inputContent)
      } else if (this.decodeType === 'list') {
        this.outputContent = decodeFunction.dec_list(this.inputContent)
      } else if (this.decodeType === 'beautify') {
        this.outputContent = global.js_beautify(this.inputContent, {unescape_strings:true})
      } else if (this.decodeType === 'uglify') {
        this.outputContent = global.UglifyJS.minify(this.inputContent).code
      }
    },
  }
}
</script>

<style scoped lang="less">
@import "~@/assets/css/_mixins.less";
@import "~@/assets/css/_color.less";
.cmp-sojson-decode {
  height: 100%;
  fieldset {
    height: 100%;
    .fieldset-content {
      height: calc(100% - 2em);
      textarea {
        display: block;
        height: 100%;
      }
    }
  }
  .form-group-list {
    height: 100%;
    padding: .5vw;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
</style>
